package com.loper7.base.utils.loadsir;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.kingja.loadsir.callback.Callback;
import com.loper7.base.R;

import java.lang.reflect.Field;

/**
 * Description:TODO
 * Create Time:2017/9/2 16:17
 * Author:KingJA
 * Email:kingjavip@gmail.com
 */

public class TimeoutCallback extends Callback {


    private View animateView;

    @Override
    protected int onCreateView() {
        return R.layout.view_time_out;
    }

    @Override
    protected void onViewCreate(Context context, View view) {
        super.onViewCreate(context, view);
        view.findViewById(R.id.timeOut_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((OnReloadListener) get(TimeoutCallback.this, "onReloadListener")).onReload(view);
            }
        });
    }

    @Override
    protected boolean onReloadEvent(Context context, View view) {
        return true;
    }

    @Override
    public void onAttach(Context context, View view) {
        animateView = view.findViewById(R.id.dataException_img);
        AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(animateView, "scaleX", 0.8f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(animateView, "scaleY", 0.8f, 1f);

        animatorSetsuofang.setDuration(600);
        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
        animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
        animatorSetsuofang.start();

    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (animateView != null) {
            animateView.clearAnimation();
        }
    }


    public static Object get(Object instance, String variableName) {
        Class targetClass = instance.getClass().getSuperclass();
        // YourSuperClass 替换为实际的父类名字
        Callback superInst = (Callback) targetClass.cast(instance);
        Field field;
        try {
            field = targetClass.getDeclaredField(variableName);
            //修改访问限制
            field.setAccessible(true);
            // superInst 为 null 可以获取静态成员
            // 非 null 访问实例成员
            return field.get(superInst);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
